package cn.iocoder.yudao.module.fm.excel;

import cn.iocoder.yudao.module.fm.controller.admin.fileinfo.vo.FileInfoExcelVO;
import cn.iocoder.yudao.module.fm.controller.admin.tunnel.vo.TunnelExcelVO;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfo.FileInfoDO;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author SUN
 * @date 2023/11/19
 */
public class Main {


    @SneakyThrows
    public static void main1(String[] args) {

        TunnelExcelVO tunnelExcelVO = new TunnelExcelVO();
        tunnelExcelVO.setNo("1");
        tunnelExcelVO.setMaterial("2");

        List<URL> urlList = Arrays.asList(
                new URL("https://sixsixsix516.oss-cn-beijing.aliyuncs.com/1.xlsx"),
                new URL("https://tse1-mm.cn.bing.net/th/id/OIP-C.mH9YLFEL5YdVxJM82mjVJQAAAA?dpr=1.1&pid=ImgDetMain"),
                new URL("https://ts3.cn.mm.bing.net/th?id=OIP-C.LH5NQfwpWSFoFF77TCf4JQHaF3&w=280&h=222&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2"),
                new URL("https://ts3.cn.mm.bing.net/th?id=OIP-C.nkWmM-lReaN8kH-ieXmZrQHaEo&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2")
        );


        List<TunnelExcelVO> list = new ArrayList<>();
        for (int i = 0; i < urlList.size(); i++) {
//            URL url = urlList.get(i);
            String url = urlList.get(i).toString();

            if (i == 0) {
                tunnelExcelVO.setImages(url);
                list.add(tunnelExcelVO);
            } else {
                TunnelExcelVO t = new TunnelExcelVO();
                t.setImages(url);
                list.add(t);
            }
        }
//
//
//        List<TunnelExcelVO> list = urlList.stream().map(url -> {
//            TunnelExcelVO t = new TunnelExcelVO();
//            BeanUtils.copyProperties(tunnelExcelVO, t);
//            t.setImages(url);
//            return t;
//        }).collect(Collectors.toList());

//        tunnelExcelVO.setImages(new URL("https://tse1-mm.cn.bing.net/th/id/OIP-C.mH9YLFEL5YdVxJM82mjVJQAAAA?dpr=1.1&pid=ImgDetMain"));

//        List<TunnelExcelVO> list = Arrays.asList(tunnelExcelVO);

        EasyExcel.write(
                        "D:\\test\\1.xlsx",
                        TunnelExcelVO.class)
                .registerWriteHandler(new CustomCellWriteHandler())
                .useDefaultStyle(false)
                .sheet("隧道信息")
                .doWrite(list);

    }

    /**
     * 按照模板导出
     */
    public static void main3(String[] args) {

        TunnelExcelVO tunnelExcelVO = new TunnelExcelVO();
        tunnelExcelVO.setNo("51666");


        FileInfoExcelVO fileInfoExcelVO = new FileInfoExcelVO();
        fileInfoExcelVO.setNo("1");
        fileInfoExcelVO.setFileName("文件名1");
        fileInfoExcelVO.setFileList("http://1.jpg\nhttp://2.jpg\nhttp://2.jpg\nhttp://2.jpg");

        FileInfoExcelVO fileInfoExcelVO2 = new FileInfoExcelVO();
        fileInfoExcelVO2.setNo("2");
        fileInfoExcelVO2.setFileName("文件名2");
        fileInfoExcelVO2.setFileList("http://1.jpg\nhttp://2.jpg\nhttp://2.jpg\nhttp://2.jpg\nhttp://2.jpg\nhttp://2.jpg12321321321321321321321321312321321\nhttp://2.jpg\nhttp://2.jpg\nhttp://2.jpg");

        List<FileInfoExcelVO> list = Arrays.asList(fileInfoExcelVO, fileInfoExcelVO2);


        ExcelWriter excelWriter = EasyExcel.write(new File("D:\\test\\2.xlsx"))
                .withTemplate(new File("D:\\test\\tunnel-export-template.xlsx"))
                .build();


        // 创建 sheet
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "第一个").build();

        // 渲染主表信息
        excelWriter.fill(tunnelExcelVO, writeSheet);

        // 子表渲染
        excelWriter.fill(list, writeSheet);

        excelWriter.finish();

    }

    /**
     * 导入
     */
    @SneakyThrows
    public static void main(String[] args) {
        List<LinkedHashMap<Long, String>> readList = EasyExcel.read(new BufferedInputStream(Files.newInputStream(new File("D:\\test\\2.xlsx").toPath()))).doReadAllSync();
        System.out.println(readList);
        System.out.println();
    }
}
